clear
clear matrix
clear mata
set matsize 11000
set maxvar 30000
set more off, perm
cd /Users/zachbrown/Projects/PriceTransparency/Data/
sysdir set PLUS /Users/zachbrown/Projects/PriceTransparency/Code/ado


// open visit dataset
guse clean/radiology_visit_2.dta, clear

drop if year>2011

// Proc class
label define procclass 1 "Bone Density" 2 "CT" 3 "Mommogram" 4 "MR" 5 "Nuclear" 6 "PET" 7 "US" 8 "X-Ray"
label values proc_class procclass
tab proc_class
tab proc_class, nolab
keep if inlist(proc_class,2,4,8) // For main analysis, use CT, MRI, and X-Ray.

drop ynch*

drop if n_procs_radio~=n_procs_on_web & n_procs_on_web>0
drop if overnight==1

drop if amt_member_oop<0
drop if amt_paid<0

// Merge price index and inflation adjust
merge m:1 year using price_index/cpi_hcs_index.dta
drop if _merge==2
drop _merge

replace amt_member_oop = amt_member_oop/price_index
replace amt_allowed = amt_allowed/price_index
replace amt_billed = amt_billed/price_index
replace amt_paid = amt_paid/price_index
replace amt_prepaid = amt_prepaid/price_index
replace amt_copay = amt_copay/price_index
replace amt_prepaid = amt_prepaid/price_index
replace amt_deduct = amt_deduct/price_index
  
replace amt_oop_radio = amt_oop_radio/price_index
replace amt_paid_radio = amt_paid_radio/price_index
replace amt_oop_radio_max = amt_oop_radio_max/price_index
replace amt_paid_radio_max = amt_paid_radio_max/price_index
replace amt_billed_radio = amt_billed_radio/price_index
replace amt_billed_radio_max = amt_billed_radio_max/price_index
replace amt_billed_on_web = amt_billed_on_web/price_index
replace amt_allowed = amt_allowed/price_index
replace amt_allowed_radio = amt_allowed_radio/price_index
replace amt_allowed_radio_max = amt_allowed_radio_max/price_index

drop price_index



// Condition outcomes
gen cond_copay = amt_copay if has_copay==1 & past_deduct~=0
gen cond_coins = amt_coins if has_coin==1 & past_deduct~=0
gen cond_deduct = amt_deduct if past_deduct==0

// Log outcomes 
gen ln_amt_member_oop=log(1+amt_member_oop)
gen ln_amt_oop_radio=log(1+amt_oop_radio)
gen ln_amt_billed    =log(1+amt_billed)
gen ln_amt_billed_radio    =log(1+amt_billed_radio)
gen ln_amt_billed_radio_max    =log(1+amt_billed_radio_max)
gen ln_amt_paid      =log(1+amt_paid)
gen ln_amt_paid_radio=log(1+amt_paid_radio)
gen ln_amt_allowed=log(1+amt_allowed)
gen ln_amt_allowed_radio=log(1+amt_allowed_radio)
gen ln_amt_allowed_radio_max=log(1+amt_allowed_radio_max)
gen ln_cond_copay     =log(1+cond_copay)
gen ln_cond_coins     =log(1+cond_coins)
gen ln_cond_deduct    =log(1+cond_deduct)

// Drop when procedure is very rare
sort proc_id_radio
by proc_id_radio: egen n_proc_tot = count(from_date)
drop if n_proc_tot<=1
drop n_proc_tot

gen deduct_type = past_deduct
replace deduct_type=2 if deduct_type==.

gen prov_id = serv_prov_cw_key2

count

// Labels
// Fix labels
label var zip_mean_income "Zip Mean Income"
label var zip_pct_hs "Zip Percent More than HS"
label var zip_pct_ba "Zip Percent More than BA"
label var charlindex "Charlson Comorbidity Index"


save clean/radiology_visit_3.dta, replace

